<template>
  <el-container class="main_con">
    <div class="gift_body">
      <el-form ref="formRules" size="small" :model="myform" label-width="100px" class="form_box" :rules="formRules">
        <div class="setting_body_item">
          <div class="header">新增链接</div>
          <div class="conetnt edit_con">
            <div class="edit_content">
              <el-form-item label="选择链接" prop="url_link">
                <el-input disabled v-model="linkdata.name" placeholder="请选择链接地址" style="width:360px;">
                  <template slot="append">
                    <span @click="selectlink">选择链接</span>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item label="有效时间">
                <el-radio-group v-model="myform.day">
                  <el-radio :label="3">3天</el-radio>
                  <el-radio :label="7">7天</el-radio>
                  <el-radio :label="15">15天</el-radio>
                  <el-radio :label="30">30天</el-radio>
                  <el-radio :label="-1">永久</el-radio>
                </el-radio-group>
              </el-form-item>
            </div>
          </div>
        </div>
      </el-form>
      <div class="form_button"><el-button type="primary" @click="addsave">保存</el-button></div>
    </div>
    <el-dialog class="selectlink" title="链接选择器" :visible.sync="drawer" destroy-on-close width="800px">
      <SelectLink ref="selectlink" @closepup="drawer=false" :linktype="linktype" :linkdata="linkdata" @changelink="changelink"></SelectLink>
    </el-dialog>
  </el-container>
</template>

<script>
import SelectLink from '@/components/SelectLink/index';
import {add} from '@/api/mall/wxurllink';

export default {
  data() {
    return {
      drawer: false,
      linkdata: {
        link: ''
      },
      linktype: ['1', '2', '3', '4', '5', '7'],
      myform:{
        day:3,
        url_link:''
      },
      formRules: {
        url_link: [
          {
            required: true,
            message: '请选择链接',
            trigger: 'blur'
          }
        ],
        }
    };
  },
  created() {},

  components: {
    SelectLink
  },
  methods: {
    //选择链接
    selectlink() {
      this.drawer = true;
      this.$nextTick(() => {
        this.$refs.selectlink.init();
      });
    },
    geturl(link){
      if (link.indexOf('../order') > -1||link.indexOf('../groupdetial') > -1) {
      	var link = link.replace('..', '/orderpage')
      } else if (link.indexOf('../agent') > -1) {
      	var link = link.replace('..', '/agentpage')
      } else if (link.indexOf('../marketing') > -1 || link.indexOf('../prize') > -1) {
      	var link = link.replace('..', '/marketpage')
      } else if (link.indexOf('../user') > -1 || link.indexOf('../rewardList') > -1 || link.indexOf(
      	'../welfare') > -1 || link.indexOf('../buymember') > -1 || link.indexOf('../pay') > -1 || link.indexOf(
      		'../afterpay') > -1 || link.indexOf('../record') > -1 || link.indexOf('../recordDetails') > -1) {
      	var link = link.replace('..', '/userpage')
      } else if (link.indexOf('../goods') > -1) {
      	var link = link.replace('..', '/pointsmallpage')
      } else {
      	var link = link.replace('..', '/pages')
      }
      return link
    },
    changelink(data) {
      this.linkdata = data;
    this.myform.url_link=this.geturl(data.link)
      this.drawer = false;
    },
    editsave() {
      let data = {
        is_show: [this.myform.is_show, this.myform.is_show_time],
        end_time: this.myform.end_time,
        id: this.id
      };
      editgroup(data).then(res => {
        this.$router.push({
          path: '/groupon/list'
        });
      });
    },
    // 添加
    addsave() {
  let _this = this;
  this.$refs['formRules'].validate(valid => {
    if (valid) {
      _this.save();
    }
  });
    },
    save() {
      let obj = {
        day:this.myform.day,
        url_link:this.myform.url_link,
        title:this.linkdata.name
      };

      add(obj).then(res => {
        // this.$message.success('创建成功');
        this.$router.push({
          path: '/wxurllink/list'
        });
      });
    },

  }
};
</script>
<style lang="scss" scoped>
.main_con {
  height: 100%;
  .gift_body {
    background-color: #ffffff;
    padding: 20px 30px;
  }
}
.setting_body_item {
  .header {
    background: #e0e3e8;
    height: 40px;
    line-height: 40px;
    padding-left: 20px;
    margin-top: 20px;
  }
  .btns_div {
    margin: 20px 0;
    text-align: center;
  }
}
</style>
